/*
 * Copyright (C) 2006 <author>
 * 
 * This file is part of FreeQueryBuilder
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * 
 * <mail>
 * 
 */

package net.sourceforge.querybuilder;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JToolBar;

import javax.swing.border.EmptyBorder;

public class MainToolbar extends BasePanel
{
	private MainWindow window;

	private Box databasebar;
	private JToolBar toolbar;
	
	MainToolbar(MainWindow syswin)
	{
		super(8,8);
		this.window = syswin;
		
		createToolbar();
		createDatabasebar();
		
		BasePanel pnlCenter = new BasePanel(8,8);
		pnlCenter.setComponentCenter(databasebar);
		pnlCenter.setComponentEast(new MainMemory());
		
		setComponentWest(toolbar);
		setComponentCenter(pnlCenter);
	}
	
	private JLabel dbstatus		= new JLabel("");
	private JLabel dbprofile	= new JLabel("<none>");
	private JLabel dbuser		= new JLabel("<none>");
	private void createDatabasebar()
	{
		databasebar = new Box(BoxLayout.X_AXIS);
//		databasebar.setBorder(new CompoundBorder(new EmptyBorder(2,0,2,0), new EtchedBorder(EtchedBorder.RAISED)));
		
		databasebar.add(dbstatus);
		dbstatus.setBorder(new EmptyBorder(3,4,2,4));
//		dbstatus.setIcon(ImageStore.getIcon("database.disconnect"));

		databasebar.add(new JLabel("database = "));
		databasebar.add(dbprofile);
		
		databasebar.add(Box.createHorizontalGlue());
		
		databasebar.add(new JLabel("user = "));
		databasebar.add(dbuser);
		
		databasebar.add(new JToolBar.Separator());
	}
	
	void connectionChanged()
	{
		if(window.builder.getConnection() == null)
		{
//			dbstatus.setIcon(ImageStore.getIcon("database.disconnect"));
			dbprofile.setText("<none>");
			dbuser.setText("<none>");
		}
		else
		{
//			dbstatus.setIcon(ImageStore.getIcon("database.connect"));
//			dbprofile.setText(syswin.builder.url);
//			dbuser.setText(syswin.builder.uid);
		}
	}	
	
	private void createToolbar()
	{
		toolbar = new JToolBar();
		toolbar.setFloatable(false);
		
		toolbar.add(new ActionOpenAdministrator());
		toolbar.addSeparator();
	}

	private class ActionOpenAdministrator extends AbstractAction
	{
		/**
		 * 
		 */
		private static final long serialVersionUID = 6332576910819399374L;

		ActionOpenAdministrator()
		{
//			this.putValue(SMALL_ICON, ImageStore.getIcon("database.jdbc"));
			this.putValue(SHORT_DESCRIPTION, "open JDBC administrator window");
		}
		
		@SuppressWarnings("deprecation")
		public void actionPerformed(ActionEvent e)
		{
//			new MaskConnect(MainToolbar.this.window.builder).show();
			new MaskConnect(MainToolbar.this.window).show();
			
		}
	}
}